package com.example.main.mypackage;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import java.io.Serializable;

public class MyChildView extends View implements Serializable {

    //记录坐标位置
    private int lastX = 0;
    private int lasty = 0;

    //字体宽高
    private int fontW = 0;
    private int fontH = 0;

    //当前文字的位置 layout
    int left = 0;
    int top = 0;
    int right = 0;
    int bottom = 0;

    private float distance;
    private float baseline;

    private RectF rectF;
    private Paint bgRect;

    private Paint paint;
    private String content = "";


    public MyChildView(Context context) {
        super(context);
        init();
    }

    public MyChildView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MyChildView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    /**
     * 初始内容
     */
    private void init() {
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setStyle(Paint.Style.FILL);
        paint.setStrokeWidth(8);
        paint.setTextSize(50);
        paint.setTextAlign(Paint.Align.CENTER);

        bgRect = new Paint();
        bgRect.setStyle(Paint.Style.FILL);
        bgRect.setColor(Color.YELLOW);
    }

    /**
     * 设置内容
     *
     * @param content
     */
    public void setContent(String content) {
        this.content = content;
        Rect rect = new Rect();
        paint.getTextBounds(content, 0, content.length(), rect);
        fontW = rect.width();
        fontH = rect.height();
        rectF = new RectF(0, 0, fontW, fontH);

        //计算baseline
        Paint.FontMetrics fontMetrics = paint.getFontMetrics();
        distance = (fontMetrics.bottom - fontMetrics.top) / 2 - fontMetrics.bottom;
        baseline = rectF.centerY() + distance;

        postInvalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawRect(rectF, bgRect);
        canvas.drawText(content, rectF.centerX(), baseline, paint);
        Log.i("move1", rectF.centerX() + " " + baseline);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(fontW, fontH);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int x = (int) event.getX();
        int y = (int) event.getY();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN: //记录触摸点的坐标
                lastX = x;
                lasty = y;
                break;
            case MotionEvent.ACTION_MOVE:
                getParent().requestDisallowInterceptTouchEvent(true);
                int offsetX = x - lastX;
                int offsetY = y - lasty;
                left = getLeft() + offsetX;
                top = getTop() + offsetY;
                right = getRight() + offsetX;
                bottom = getBottom() + offsetY;
                setLayout();
                break;
            case MotionEvent.ACTION_UP:
                performClick();
                getParent().requestDisallowInterceptTouchEvent(false);
                break;
        }
        return true;
    }

    /**
     * 设置布局
     */
    public void setLayout() {
        Log.i("move2", left + " " + top);
        layout(left, top, right, bottom);
    }
}
